package com.dingding.common.core.util.prop;

import java.util.concurrent.ConcurrentHashMap;

public class PropKit {

	/**
	 * 单个proprties对象集合
	 */
	private static Prop prop = null;

	/**
	 * 多个properties对象集合
	 */
	private static final ConcurrentHashMap<String, Prop> map = new ConcurrentHashMap<String, Prop>();

	private PropKit() {

	}

	/**
	 * 
	 * 加载properties文件,默认编码为UTF-8
	 * 
	 * @Title: use 
	 * @param fileName 文件名称
	 * @return  Prop 加载后的properties对象
	 * @author CHEHAUNBO
	 * 
	 */
	public static Prop use(String fileName) {
		return use(fileName, "UTF-8");
	}

	/**
	 * 加载properties文件
	 * 
	 * Example:
	 * PropKit.use("config.txt", "UTF-8");
	 * PropKit.use("other_config.txt", "UTF-8");
	 * String userName = PropKit.get("userName");
	 * String password = PropKit.get("password");
	 * 
	 * userName = PropKit.use("other_config.txt").get("userName");
	 * password = PropKit.use("other_config.txt").get("password");
	 * 
	 * PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
	 * 
	 * @param fileName 文件名称
	 * @param encoding 编码
	 * 
	 */
	public static Prop use(String fileName, String encoding) {
		Prop result = map.get(fileName);
		if (result == null) {
			result = new Prop(fileName, encoding);
			map.put(fileName, result);
			if (PropKit.prop == null)
				PropKit.prop = result;
		}
		return result;
	}

	public static Prop getProp() {
		if (prop == null)
			throw new IllegalStateException(
					"Load propties file by invoking PropKit.use(String fileName) method first.");
		return prop;
	}

	public static Prop getProp(String fileName) {
		return map.get(fileName);
	}

	public static String get(String key) {
		return getProp().get(key);
	}

	public static String get(String key, String defaultValue) {
		return getProp().get(key, defaultValue);
	}

	public static Integer getInt(String key) {
		return getProp().getInt(key);
	}

	public static Integer getInt(String key, Integer defaultValue) {
		return getProp().getInt(key, defaultValue);
	}

	public static Long getLong(String key) {
		return getProp().getLong(key);
	}

	public static Long getLong(String key, Long defaultValue) {
		return getProp().getLong(key, defaultValue);
	}

	public static Boolean getBoolean(String key) {
		return getProp().getBoolean(key);
	}

	public static Boolean getBoolean(String key, Boolean defaultValue) {
		return getProp().getBoolean(key, defaultValue);
	}

	public static boolean containsKey(String key) {
		return getProp().containsKey(key);
	}
}
